package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ExportDao;
import cn.itcast.dao.cargo.ExportProductDao;
import cn.itcast.dao.cargo.PackingDao;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.vo.PackingListResult;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@Service
public class PackingServiceImpl implements PackingService {

    @Autowired
    private PackingDao packingDao;

    @Autowired
    private ExportDao exportDao;

    @Autowired
    private ExportProductDao exportProductDao;

    @Autowired
    private ContractDao contractDao;

    @Override
    public PageInfo findAll(int page, int size, PackingExample example) {
        PageHelper.startPage(page,size);
        List<Packing> list = packingDao.selectByExample(example);
        return new PageInfo(list);
    }

    @Override
    public String save(Packing packing, String[] eId) {
        if (eId == null){
            return "装箱单传入有误";
        }
        String msg = "";
        String exportNos = "";
        String eids = Arrays.toString(eId);
        String exportId = eids.substring(1,eids.length()-1).replaceAll(" ","");

        double grossWeights = 0d;
        double netWeights = 0d;
        double totalVolume = 0d;

        List<Export> list = new ArrayList();
        Export export = exportDao.selectByPrimaryKey(eId[0]);
        Export exportNext = null;

        if(eId.length>1) {

            for (int i = 0; i < eId.length - 1; i++) {
                exportNext = exportDao.selectByPrimaryKey(eId[i + 1]);
                if (!export.getConsignee().equals(exportNext.getConsignee())) {
                    return "收货人不同 ";
                } else if (!export.getDestinationPort().equals(exportNext.getDestinationPort())) {

                    return "目的地不同 ";
                } else if (!export.getShipmentPort().equals(exportNext.getShipmentPort())) {

                    return "装运地不同";
                }
                list.add(exportNext);
            }
        }
        list.add(export);

        for (int i = 0; i < list.size(); i++) {
            ExportProductExample example = new ExportProductExample();
            ExportProductExample.Criteria criteria = example.createCriteria();
            criteria.andExportIdEqualTo(list.get(i).getId());
            List<ExportProduct> exportProducts = exportProductDao.selectByExample(example);
            for (ExportProduct product : exportProducts) {
                try {
                    grossWeights += product.getGrossWeight();
                    netWeights += product.getNetWeight();
                }catch (Exception e){
                    return "货物毛重或净重数据异常";
                }
            }

            totalVolume += list.get(i).getMeasurements();
            String[] split = list.get(i).getContractIds().split(",");
            for (int j = 0; j < split.length; j++) {
                Contract contract = contractDao.selectByPrimaryKey(split[j]);
                exportNos+=contract.getContractNo()+"|";
            }
        }

        packing.setId(UUID.randomUUID().toString());
        packing.setExportIds(exportId);
        packing.setExportNos(exportNos.substring(0,exportNos.length() - 1));
        packing.setState(0);
        packing.setGrossWeights(grossWeights);
        packing.setNetWeights(netWeights);
        packing.setTotalVolume(totalVolume);
        packingDao.insert(packing);

        return msg;
    }


    //查询列表
    @Override
    public List<Packing> findAll(PackingExample example) {
        return packingDao.selectByExample(example);
    }

    @Override
    public void update(Packing packing) {

    }

    @Override
    public void delete(String id) {
        Packing packing = packingDao.selectByPrimaryKey(id);
        Integer state = packing.getState();
        if (state.equals(0)){
            packingDao.deleteByPrimaryKey(id);
        }
    }

    @Override
    public Packing findById(String id) {
        return packingDao.selectByPrimaryKey(id);
    }

    @Override
    public PackingListResult findById(String[] id) {
        PackingListResult result = new PackingListResult();
        List<ExportProduct> list = new ArrayList<>();
        Packing packing = null;

        for (int i = 0; i < id.length; i++) {
            packing = packingDao.selectByPrimaryKey(id[i]);
            String[] split = packing.getExportIds().split(",");
            for (String eId : split) {
                ExportProductExample example = new ExportProductExample();
                ExportProductExample.Criteria criteria = example.createCriteria();
                criteria.andExportIdEqualTo(eId);
                List<ExportProduct> exportProduct = exportProductDao.selectByExample(example);
                for (ExportProduct product : exportProduct) {
                    list.add(product);
                }
            }
        }
        result.setSeller(packing.getSeller());
        result.setBuyer(packing.getBuyer());
        result.setList(list);
        return result;
    }
}
